﻿/*************************************************************************************
 * 文 件 名:   HK_UIReplace
 * 
 * 描    述:  将选中的Button或Toggle，替换为HK_Button、HK_Toggle
 * 
 * 版    本：  V1.0
 * 创 建 者：  周阿媛、京产肠饭
 * 创建时间：  2022/1/9
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.UI.Button;
using static UnityEngine.UI.Selectable;
using static UnityEngine.UI.Toggle;

public class HK_UIReplace : EditorWindow
{
    bool lastInteractable;
    Transition lastTransition;
    ColorBlock lastColorBlock;
    SpriteState lastSpriteState;
    Navigation lastNavigation;
    Graphic lastTargetGraphic;
    AnimationTriggers lastAnimTriggers;
    ButtonClickedEvent lastButtonClicked;

    [MenuItem("HKTools/UIReplace")]
    static void Init()
    {
        HK_UIReplace resetNameWindow = GetWindow<HK_UIReplace>("ReplaceUIComponent");
        resetNameWindow.Show();
    }

    void OnGUI()
    {
        EditorGUILayout.Space(15);

        EditorGUILayout.LabelField("是否替换当前选中的" + Selection.objects.Length + "个UI组件？");
        EditorGUILayout.Space(10);

        if (GUILayout.Button("替换为HK_Button", GUILayout.Width(150), GUILayout.Height(30)))
        {
            SelectObj();
            ReplaceButton();
        }

        EditorGUILayout.Space(10);

        if (GUILayout.Button("替换为HK_Toggle", GUILayout.Width(150), GUILayout.Height(30)))
        {
            SelectObj();
            ReplaceToggle();
        }
    }

    void SelectObj()
    {
        if (Selection.gameObjects.Length <= 0)
        {
            Debug.Log("<color=red>错误：</color>" + "没有选中任何物体");
            return;
        }
    }

    string succeedTips = "";
    string failedTips = "";

    //button替换
    void ReplaceButton()
    {
        succeedTips = "";
        failedTips = "";

        //button替换
        for (int i = 0; i < Selection.gameObjects.Length; i++)
        {
            Button lastBtn = Selection.gameObjects[i].GetComponent<Button>();

            if (lastBtn)
            {
                lastInteractable = lastBtn.interactable;
                lastTransition = lastBtn.transition;
                lastSpriteState = lastBtn.spriteState;
                lastAnimTriggers = lastBtn.animationTriggers;
                lastColorBlock = lastBtn.colors;
                lastTargetGraphic = lastBtn.targetGraphic;
                lastNavigation = lastBtn.navigation;
                lastButtonClicked = lastBtn.onClick;

                //销毁原Button
                Undo.DestroyObjectImmediate(lastBtn);

                //添加HK_Button
                Button newBtn = Undo.AddComponent(Selection.gameObjects[i], typeof(HK_Button)) as Button;

                //复制组件属性到HK_Button
                newBtn.interactable = lastInteractable;
                newBtn.transition = lastTransition;
                newBtn.spriteState = lastSpriteState;
                newBtn.animationTriggers = lastAnimTriggers;
                newBtn.colors = lastColorBlock;
                newBtn.targetGraphic = lastTargetGraphic;
                newBtn.navigation = lastNavigation;
                newBtn.onClick = lastButtonClicked;

                succeedTips += newBtn.name + "  ";
            }
            else
            {
                failedTips += Selection.gameObjects[i].name + "  ";
            }
        }
        EditorUtility.DisplayDialog("提示", "成功替换组件的名字为：" + succeedTips + "\n替换失败的名字为：" + failedTips, "确认");
    }

    bool lastIsOn;
    Graphic lastGraphic;
    ToggleGroup lastToggleGroup;
    ToggleEvent lastToggleEvent;
    ToggleTransition lastToggleTransition;

    //toggle替换
    void ReplaceToggle()
    {
        succeedTips = "";
        failedTips = "";

        for (int i = 0; i < Selection.gameObjects.Length; i++)
        {
            Toggle lastTog = Selection.gameObjects[i].GetComponent<Toggle>();
            if (lastTog)
            {
                lastInteractable = lastTog.interactable;
                lastTransition = lastTog.transition;
                lastSpriteState = lastTog.spriteState;
                lastAnimTriggers = lastTog.animationTriggers;
                lastTargetGraphic = lastTog.targetGraphic;
                lastColorBlock = lastTog.colors;
                lastNavigation = lastTog.navigation;
                lastIsOn = lastTog.isOn;
                lastToggleTransition = lastTog.toggleTransition;
                lastGraphic = lastTog.graphic;
                lastToggleGroup = lastTog.group;
                lastToggleEvent = lastTog.onValueChanged;

                //销毁原Toggle
                Undo.DestroyObjectImmediate(lastTog);

                //添加HK_Toggle
                Toggle newTog = Undo.AddComponent(Selection.gameObjects[i], typeof(HK_Toggle)) as Toggle;

                //复制组件属性到HK_Toggle
                newTog.interactable = lastInteractable;
                newTog.transition = lastTransition;
                newTog.spriteState = lastSpriteState;
                newTog.animationTriggers = lastAnimTriggers;
                newTog.targetGraphic = lastTargetGraphic;
                newTog.colors = lastColorBlock;
                newTog.navigation = lastNavigation;
                newTog.isOn = lastIsOn;
                newTog.toggleTransition = lastToggleTransition;
                newTog.graphic = lastGraphic;
                newTog.group = lastToggleGroup;
                newTog.onValueChanged = lastToggleEvent;

                succeedTips += newTog.name + "  ";
            }
            else
            {
                failedTips += Selection.gameObjects[i].name + "  ";
            }
        }
        EditorUtility.DisplayDialog("提示", "成功替换组件的名字为：" + succeedTips + "\n替换失败的名字为：" + failedTips, "确认");
    }
}




